package app

import com.qc.Builds
import com.qc.Reports

class MetricsController {
    static layout = "mertics"
    def m = {
        Reports.list().each {
            it.lcom = 700 * Random.newInstance().nextFloat() /10
            it.comments = 450 * Random.newInstance().nextFloat()/10
            it.coverage = 990 * Random.newInstance().nextFloat()/10
        }
        render 4
    }
    def resultingService
    def classesLength = { chainAction('loc', params.action) }
    def methodsLength = { chainAction('favg', params.action) }
    def LCOM = { chainAction('lcom', params.action) }
    def complexity = { chainAction('complexity', params.action) }
    def avgComplexity = { chainAction('favg', params.action) }
    def coverage = { chainAction2('coverage', params.action) }
    def comments = {
        chainAction2('comments', params.action)
    }

    def setLast = {
        log.error(session.chosenBuild)
        session.chosenBuild = Builds.get(params.buildChooser)
        redirect(action: methodsLength)
    }

    def fileOutput = {
        def file = new File(params.fileName)
        render "<pre>"
        int lineNumber = 0
        if (file) {
            file.eachLine {
                lineNumber++
                render "$lineNumber \t $it<br/>"
            }
        }
        render "</pre>"
    }


    def common = {
        def last = Builds.last()
        Builds previous = session.chosenBuild
        def nloc = 0
        def favg = 0
        last.reports.each { nloc += it.loc }
        last.reports.each { favg += it.favg }

        [nloc: nloc, favg: favg]
    }


    def index() {
//        resultingService.xmlParsing()
        resultingService.createTestData()
        redirect(action: methodsLength)
    }

    def chainAction(fieldName, view) {
        def last = Builds.last()
        params.max = params.max ?: 10
        params << [order: 'desc', sort: fieldName]
        def averageVal = calculateAvgValue(last, fieldName)
        def valueLimit = averageVal * 1.5
        def resultsList = Reports.findAllByAnalysis(last, params).collect {
            [
                    name: it.file.name,
                    path: it.file.fullPath,
                    value: it[fieldName],
                    changed: fakeRandomValue()
            ]
        }
        render(view: view, model: [
                filesList: resultsList,
                valueLimit: valueLimit,
                averageValue: averageVal,
                resultsCount: Reports.countByAnalysis(last)
        ])
    }

    def chainAction2(fieldName, view) {
        def last = Builds.last()
        params.max = params.max ?: 10
        params << [order: 'asc', sort: fieldName]
        def averageVal = calculateAvgValue(last, fieldName)
        def valueLimit = averageVal / 1.5
        def resultsList = Reports.findAllByAnalysis(last, params).collect {
            [
                    name: it.file.name,
                    path: it.file.fullPath,
                    value: it[fieldName],
                    changed: fakeRandomValue()
            ]
        }
        render(view: view, model: [
                filesList: resultsList,
                valueLimit: valueLimit,
                averageValue: averageVal,
                resultsCount: Reports.countByAnalysis(last)
        ])
    }

    private Number calculateAvgValue(Builds build, String field) {
        def results = Reports.findAllByAnalysis(build)
        def avgValue = 0;
        results.each {
            avgValue += it[field] as Number;
        }
        if (results.size()) {
            avgValue /= results.size()
        }
        avgValue
    }

    private fakeRandomValue() {
        def random = new Random()
        if (!random.nextInt(3)) {
            random.nextInt(8) - 4
        } else {
            0
        }
    }

}
